package com.apkcore.core.ui.dialog

import android.app.Dialog
import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.Window
import android.view.WindowManager
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.viewbinding.ViewBinding
import com.apkcore.core.viewbinding.viewBinding


class EasyDialog<VB : ViewBinding>(
    private val owner: LifecycleOwner,
    inflate: (LayoutInflater) -> VB,
    private val width: Int? = null,
    private val height: Int? = null,
    private val canceledOnTouchOutside: Boolean = true
) : Dialog(
    when (owner) {
        is FragmentActivity -> {
            owner
        }

        is Fragment -> {
            owner.requireContext()
        }

        else -> {
            throw Throwable("只能传Fragment和activity")
        }
    }
) {

    val binding: VB by viewBinding(inflate)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        setContentView(binding.root)

        owner.lifecycle.addObserver(object : DefaultLifecycleObserver {
            override fun onStop(owner: LifecycleOwner) {
                super.onStop(owner)
                dismiss()
            }
        })

        window?.let { window ->
            window.setLayout(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
            )

            val layoutParams = WindowManager.LayoutParams()
            layoutParams.copyFrom(window.attributes)
            val displayMetrics = context.resources.displayMetrics
            layoutParams.width = width ?: (displayMetrics.widthPixels * 0.8f).toInt()
            if (height != null) {
                layoutParams.height = height
            }
            window.setAttributes(layoutParams)

//            it.setBackgroundDrawableResource(android.R.color.transparent)
            window.setGravity(Gravity.CENTER)
            window.attributes?.windowAnimations = android.R.style.Animation_Dialog

            val gradientDrawable = GradientDrawable()
            gradientDrawable.setColor(Color.TRANSPARENT) // 设置背景色
//            gradientDrawable.setCornerRadius(10.dp2px().toFloat()) // 设置圆角半径，可以根据需要调整
            window.setBackgroundDrawable(gradientDrawable)
//            binding.root.getSystemWindow {
//
//            }
        }
//        window?.decorView?.systemUiVisibility = (
//                View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
//                        or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
//                        or View.SYSTEM_UI_FLAG_FULLSCREEN
//                )
        window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)

        setCanceledOnTouchOutside(canceledOnTouchOutside)
        setCancelable(canceledOnTouchOutside)
    }
}
